package com.ruku.controller;

import com.bean.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruku.service.RuKuService;
import com.ruku.service.Ruku2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("ruku")
public class RuKuController {
    @Autowired
    RuKuService ruKuService;
    @Autowired
    Ruku2Service ruKu2Service;
    @RequestMapping("ruku.do")
    public String ruKu(HttpServletRequest request) {
        String ruKuId = UUID.randomUUID().toString().replace("-", "").substring(0, 11);
        System.out.println("入库库ID:"+ruKuId);
        request.getSession().setAttribute("ruKuId",ruKuId);
        List<WmsTStore> stores = ruKuService.selectStore();
        request.getSession().setAttribute("stores",stores);
        return "ruku/banli";
    }


    @RequestMapping("getMess.do")
    @ResponseBody
    public List<WmsTCustomer> getCusMess(String shuru){
//        String shuru1=new String(shuru.getBytes("ISO-8859-1"),"utf-8");
        List<WmsTCustomer> customers=ruKuService.getCusMess(shuru);

        System.out.println(customers);
        return customers;
    }



    //办理入库插入wms_t_bill表
    @RequestMapping("banli.do")
    public String insertRuku(WmsTBill wmsTBill, @PathParam("custName") String custName, HttpServletRequest request){
        //根据输入的custName获取WmsTBill表里的customerID
        WmsTCustomer wmsTCustomer=ruKuService.getCustID(custName);
        String customerID=wmsTCustomer.getCustID();
        //从域中获取userId
        WmsTUser wmsTUser= (WmsTUser) request.getSession().getAttribute("load_user");
        String billTransactor=wmsTUser.getUserID();
        System.out.println("userid"+billTransactor);
        wmsTBill.setBillEditTime(wmsTBill.getBillDate());
        //获取 库存费
        double billFeeStore=wmsTBill.getBillFeeStore()+0.00;
        //获取 力资费
        double billFeeTruckage=wmsTBill.getBillFeeTruckage()+0.00;
        //获取 其他费用
        double billFeeOther=wmsTBill.getBillFeeOther()+0.00;
        wmsTBill.setBillPayable(billFeeStore*30+billFeeTruckage+billFeeOther);
        //插入数据
        wmsTBill.setCustomerID(customerID);
        wmsTBill.setBillTransactor(billTransactor);
        System.out.println("前端获取"+wmsTBill);
        ruKuService.insertRuku(wmsTBill);
        //将所有信息放入域中
        List<WmsTBill> wmsTBills=ruKuService.selectAll();
        System.out.println("所有信息"+wmsTBills);
        request.getSession().setAttribute("wmsTBills",wmsTBills);
        return "ruku/yanhuo";
    }

//跳转装盘明细
    @RequestMapping("zhuangpan.do")
    public String zhuangPan( @PathParam("billID")String billID, String custName, HttpServletRequest request){
        System.out.println("billid"+billID);
        List<WmsTBillDetails> wmsTBillDetails=ruKuService.selectBillDetail(billID);
        System.out.println("明细"+wmsTBillDetails);
        //将信息放入域中
        request.getSession().setAttribute("custName",custName);
        request.getSession().setAttribute("billID",billID);
        request.getSession().setAttribute("wmsTBillDetails",wmsTBillDetails);
        return "ruku/zhuangpan";
    }


    //入库结算
    @RequestMapping("jiesuan.do")
    public String jiesuan(String billID, HttpServletRequest request){
        ruKuService.UpdateBill(billID);
       String billID1 = (String) request.getSession().getAttribute("billID");
        System.out.println("域中拿出来的billID1"+billID1);
        List<WmsTBillDetails> wmsTBillDetails=ruKu2Service.selectBill(billID1);
        System.out.println("三表联查"+wmsTBillDetails);
        int tuopanNum = wmsTBillDetails.size();
        request.getSession().setAttribute("waitjiesuan",wmsTBillDetails);
        request.getSession().setAttribute("tuopanNum",tuopanNum);
        return "ruku/jiesuan";
    }

    //批量删除装盘明细
    @RequestMapping("deleteMany.do")
    public String deleteMany(String detailsID, HttpServletRequest request){
        System.out.println("删除的detailsID"+detailsID);
        ruKuService.deleteMany(detailsID);
        String billID2 = (String) request.getSession().getAttribute("billID");
        List<WmsTBillDetails> wmsTBillDetails=ruKuService.selectBillDetail(billID2);
        request.getSession().setAttribute("wmsTBillDetails",wmsTBillDetails);
        return "ruku/zhuangpan";
    }

    //费用结算
    @RequestMapping("feiyong.do")
    public String feiyong(WmsTBill wmsTBill, HttpServletRequest request){
        String billID2 = (String) request.getSession().getAttribute("billID");
        wmsTBill.setBillID(billID2);
        ruKuService.feiyong(wmsTBill);
        return "ruku/queren";
    }

    //点击入库确认的时候更新wms_t_bill 的billState = 3
    @RequestMapping("rukuqueren.do")
    public String rukuqueren(HttpServletRequest request){
        String billID3 = (String) request.getSession().getAttribute("billID");
        ruKuService.rukuqueren(billID3);







        //开始分页
            PageHelper.startPage(1, 1);
        //查询数据
        List<Map<String, Object>>  all=ruKuService.bill("2022-12-4","2022-12-9","王五","1111");
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(all);
        System.out.println("分页"+pageInfo);
        request.getSession().setAttribute("pageInfoo", pageInfo);
        return "ruku/rukudan";
    }

    //入库单查询
    @RequestMapping("selectrukudan.do")
    public String bill(String start, String end, String custName, String pageSize, String pageNum, String billID,HttpServletRequest request){



        //开始分页
        if (pageSize==null || pageSize.equals("")) {
            PageHelper.startPage(1, 5);
            //pageSize & pageNum 放域
            request.getSession().setAttribute("billID", billID);
            request.getSession().setAttribute("custName", custName);
            request.getSession().setAttribute("start", start);
            request.getSession().setAttribute("end", end);
            request.getSession().setAttribute("num1", 1);
            request.getSession().setAttribute("size1", 5);
        } else {
            PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            //pageSize & pageNum 放域
            request.getSession().setAttribute("num1", Integer.parseInt(pageNum));
            request.getSession().setAttribute("size1", Integer.parseInt(pageSize));
        }

        //查询数据
        List<Map<String, Object>>  all=ruKuService.bill(start,end,custName,billID);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(all);
        System.out.println("分页"+pageInfo);
        request.getSession().setAttribute("pageInfoo", pageInfo);
        return "ruku/rukudan";
    }

    //入库单信息
    @RequestMapping("rukudanMess.do")
    @ResponseBody
    public WmsTBill rukudanMess(String billID, HttpServletRequest request){
        System.out.println("入库单billID"+billID);
        WmsTBill wmsTBill= ruKuService.selectbills(billID);
        System.out.println("入库单信息"+wmsTBill);
        return wmsTBill;
    }

    //删除入库单信息
    @RequestMapping("deleteMess.do")
    public String deleteMess(String billID ,HttpServletRequest request,String start, String end, String custName){
        System.out.println("删除入库单billID"+billID);
        ruKuService.deleteMess(billID);
        List<Map<String, Object>>  all=ruKuService.bill(start,end,custName,billID);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<Map<String, Object>>(all);
        System.out.println("分页"+pageInfo);
        request.getSession().setAttribute("pageInfoo", pageInfo);
        return "ruku/rukudan";
    }

    //更新入库单信息
    @RequestMapping("updateMess.do")
    @Transactional
    public String updateMess(String custName,String billDate,String billEditTime,double billFeeTruckage,String billID){
        System.out.println("更新"+custName);
        System.out.println("更新"+billDate);
        System.out.println("更新"+billEditTime);
        System.out.println("更新"+billFeeTruckage);
        System.out.println("更新"+billID);
        WmsTCustomer wmsTCustomer= ruKuService.selectCustID(custName);
        String customerID= wmsTCustomer.getCustID();
        System.out.println("查出来的"+customerID);
        ruKuService.updateMess(billDate,billEditTime,billFeeTruckage,billID,customerID);
        ruKuService.jisuan(billID);
        return "ruku/rukudan";
    }

}
